package com.spring.mvc.mail;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;

import com.spring.mvc.exception.MailNullException;

@Controller
public class SendMail {

	@Autowired
	private JavaMailSender mailSender;
	private MailTemplate mailTemplate;
	
	/**
	 * Send Mail
	 * @throws MailNullException
	 */
	public void sendMsg() throws MailNullException{
		if(null != mailTemplate && null != mailSender){
			if(mailTemplate.getMailMessage() instanceof SimpleMailMessage){
				mailSender.send((SimpleMailMessage)mailTemplate.getMailMessage());
			}else if(mailTemplate.getMailMessage() instanceof MimeMessage){
				mailSender.send((MimeMessage)mailTemplate.getMailMessage());
			}
		}else{
			throw new MailNullException("mail template is null");
		}
	}

	/**
	 * Set up Mail template
	 * @param mailTemplate
	 */
	public void setMailTemplate(MailTemplate mailTemplate) {
		this.mailTemplate = mailTemplate;
	}
}
